<?php
/**
 * 用户公共类
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:32
 */

namespace app\api\controller;


use common\Token;
use think\facade\Db;
use think\facade\Request;

class UserCommon extends Common
{
    public $user_id = null;
    public function __construct(){
        $token = input("token", '', 'trim');
        $token = $token ? $token : Request::header('token');
        if (empty($token)) {
            return $this->resJson(1010, '用户未登录');
        }
        $info = Token::verifyToken($token);
        if (!$info){
            return $this->resJson(1011, '无token信息', '');
        }
        $user_id = $info['user_id'];
        $user = Db::name('user')->where('status',1)->where('user_id',$user_id)->find();
        if (!$user) {
            return $this->resJson(1012, '账号不存在', '');
        }
        $this->user_id = $user_id;
    }


    /**
     * 查找判断二维数组中是否含有某个值
     * @param $value
     * @param $array
     * @return bool
     */
    public function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }
            if(in_array($value, $item)) {
                return true;
            } else if($this->deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }
}